package com.fast.admin.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fast.admin.entity.sys.Role;
import com.fast.admin.entity.sys.UserRole;
import com.fast.admin.service.sys.IRoleResourceService;
import com.fast.admin.service.sys.IRoleService;
import com.fast.admin.service.sys.IUserRoleService;
import com.fast.admin.service.sys.IUserService;
import com.fast.common.base.BaseController;
import com.fast.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Title：角色控制器
 * @author：YLC
 * @date：2018/8/1 16:21
 * @version：1.0
 **/
@Controller
@RequestMapping(value = "/sys/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRoleResourceService roleResourceService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IUserService userService;
    @Override
    protected String getPrefix() {
        return "sys/role/";
    }

    @GetMapping(value = "/page")
    @ResponseBody
    public R page(@RequestParam Map<String, Object> params, Role role){
        return R.ok(roleService.findPage(params, new QueryWrapper<>(role)));
    }

    @ResponseBody
    @PostMapping(value = "/save")
    public R save(Role role){
        roleService.addOrUpdate(role);
        return R.ok();
    }

    @ResponseBody
    @GetMapping(value = "/get/{id}")
    public  R get(@PathVariable("id") Long id){
        return R.ok(roleService.selectById(id));
    }

    @ResponseBody
    @PostMapping(value = "/del")
    public R del(String id){
        roleService.delById(id);
        return R.ok();
    }

    /**
     * 资源授权页面
     * @return
     */
    @GetMapping(value = "/resourceAuthView/{roleId}")
    public String resourceAuthView(@PathVariable("roleId")Long roleId, Model model){
        model.addAttribute("roleId", roleId);
        return getPrefix() + "resource_auth";
    }

    /**
     * 授权资源
     * @return
     */
    @PostMapping(value = "/authResource")
    @ResponseBody
    public R authResource(Long roleId, String resourceId){
        roleResourceService.insertBatchByRole(roleId, resourceId);
        return R.ok();
    }

    @GetMapping(value = "/{roleId}/userView")
    public String userView(@PathVariable("roleId")Long roleId,  Model model){
        model.addAttribute("roleId", roleId);
        return getPrefix() + "userList";
    }

    /**
     * 通过角色获取用户分页
     * @param roleId
     * @return
     */
    @GetMapping(value = "/{roleId}/userPage")
    @ResponseBody
    public R userPage(@PathVariable("roleId")Long roleId,@RequestParam Map<String, Object> pageParam){
        List<UserRole> userRoleList = userRoleService.selectByRoleId(roleId);
        if (userRoleList.isEmpty()){
            return R.ok();
        }else{
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("id", userRoleList.stream().map(UserRole::getUserId).collect(Collectors.toList()));
            return R.ok(userService.findPage(pageParam, queryWrapper));
        }
    }
}
